﻿using System;
using MayhemCore;
using MayhemCore.ModuleTypes;
using Microsoft.Phone.Controls;

namespace MayhemWP7
{
    public partial class AddConnection : PhoneApplicationPage
    {
        public AddConnection()
        {
            InitializeComponent();
        }
        
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            Mayhem m = Mayhem.Instance;
            EventList.ItemsSource = m.EventList;
            ReactionList.ItemsSource = m.ReactionList;
        }

        /// <summary>
        /// User is confirming the new connection
        /// </summary>
        private void ConfirmButton_Click(object sender, EventArgs args)
        {
            // Create a new connection and add to Mayhem
            
            // We have to clone the event and reaction
            ModuleType SelectedModule = (ModuleType)EventList.SelectedItem;
            EventBase e = (EventBase)Activator.CreateInstance(SelectedModule.Type);

            SelectedModule = (ModuleType)ReactionList.SelectedItem;
            ReactionBase reaction = (ReactionBase)Activator.CreateInstance(SelectedModule.Type);

            Mayhem.Instance.ConnectionList.Add(new Connection(e, reaction));

            // Go back to the main connection list
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }  
        }
    }
}